<template>
  <div id="resourceUpload">
    <el-form :model="resourceData" :rules="fromRules" ref="form">
      <el-form-item prop="url">
        <div>
          资源URL
          <el-input type="input" v-model="resourceData.url" placeholder="请输入资源url" clearable></el-input>
        </div>
      </el-form-item>
      <el-form-item prop="text">
        <div>
          资源说明
          <el-input type="textarea" v-model="resourceData.text" placeholder="请输入资源说明" autosize></el-input>
        </div>
      </el-form-item>
      <el-form-item prop="type">
        <div>
          资源类型
          <div class="typeBar">
            <p>1.选择已有的资源类型</p>
            <el-select v-model="resourceData.type">
              <el-option v-for="(item, index) in typeData" :key="index" :value="item.type" :label="item.type"></el-option>
            </el-select>
            <p>2.创建新的资源类型</p>
            <el-input type="input" v-model="resourceData.type" placeholder="请输入资源说明" clearable></el-input>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <el-button @click="submit" type="success">提交</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      resourceData: {
        url: '',
        text: '',
        type: ''
      }, // 输入数据
      typeData: [], // 所有资源类型数据
      fromRules: {
        url: [
          { required: true, message: '请输入资源url', trigger: 'blur' },
        ],
        text: [
          { required: true, message: '请输入资源说明', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请输入资源类型', trigger: 'blur' },
          { required: true, message: '请选择资源类型', trigger: 'change' },
        ],
      }, // 表单规则
    }
  },
  methods: {
    // 提交资源
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 准备数据
          let data = new FormData()
          data.append("url", this.resourceData.url)
          data.append("text", this.resourceData.text)
          data.append("type", this.resourceData.type)
          this.$axios({
            method: 'post',
            url: '/webResource/login/uploadResource',
            data: data,
            headers: {
              'Content-Type': 'multipart/form-data'
            }
          }).then(res => {
            if (res.data) {
              this.$message({
                message: '提交成功',
                type: 'success',
                showClose: true,
              })
            } else {
              this.$message({
                message: '提交失败！出现错误了X.X',
                type: 'error',
                showClose: true,
              })
            }
          })
        } else {
          this.$message({
            message: '请完善资源信息再提交',
            type: 'error',
            showClose: true
          })
        }
      })
    },
    // 获取所有资源类型
    getAllType() {
      this.$axios({
        method: 'post',
        url: '/webResource/login/getAllResourceType'
      }).then(res => {
        this.typeData = res.data
      })
    }
  },
  mounted() {
    this.getAllType()
  }
}
</script>

<style lang="scss" scoped>
#resourceUpload {
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.typeBar {
  padding-left: 20px;
}
</style>
<style scoped="scoped" scope>
/deep/.el-textarea__inner,
/deep/.el-input__inner {
  background-color: #202020;
  text-align: center;
  color: #fff;
}

/deep/.el-input__inner {
  width: 300px;
}

/deep/.el-textarea__inner {
  min-height: 60px !important;
}
</style>